package stu.xjtu.physical.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import stu.xjtu.physical.common.JsonResponse;
import stu.xjtu.physical.common.utls.JwtHelper;
import stu.xjtu.physical.service.OxygenInfoService;
import stu.xjtu.physical.model.domain.OxygenInfo;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 *
 *  前端控制器
 *
 *
 * @author lsong
 * @since 2024-03-31
 * @version v1.0
 */
@RestController
@Slf4j
@RequestMapping("/api/oxygenInfo")
public class OxygenInfoController {

    private final Logger logger = LoggerFactory.getLogger( OxygenInfoController.class );

    @Autowired
    private OxygenInfoService oxygenInfoService;

    @Autowired
    private JwtHelper jwtHelper;
    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        OxygenInfo  oxygenInfo =  oxygenInfoService.getById(id);
        return JsonResponse.success(oxygenInfo);
    }

    @RequestMapping(value = "/mem/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getByFamilyId(@PathVariable("id") Long id)throws Exception {
        List<OxygenInfo> oxygenInfo =  oxygenInfoService.getByFamliyId(id);
        return JsonResponse.success(oxygenInfo);
    }

    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        oxygenInfoService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateOxygenInfo(OxygenInfo  oxygenInfo) throws Exception {
        oxygenInfoService.updateById(oxygenInfo);
        return JsonResponse.success(null);
    }


    /**
    * 描述:创建OxygenInfo
    *
    */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(@RequestBody OxygenInfo  oxygenInfo, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        Long userId = jwtHelper.getFrontUserId(token);
        oxygenInfo.setUId(userId);
        log.info("{}",oxygenInfo.toString());
        oxygenInfoService.save(oxygenInfo);
        return JsonResponse.success(null);
    }
}

